package com.zjh.usercenter.controller;

/**
 * 用户贴
 *
 * @author LiZiQiu
 * @data 2023/3/22
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjh.usercenter.model.domain.User;
import com.zjh.usercenter.model.domain.request.UserLoginRequst;
import com.zjh.usercenter.model.domain.request.UserResgisterRequst;
import com.zjh.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.zjh.usercenter.controller.constant.UserConstant.Admin_Role;
import static com.zjh.usercenter.controller.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Long userRegiter(@RequestBody UserResgisterRequst userResgisterRequst) {

        if (userResgisterRequst == null) {
            return null;
        }

        String userAccount = userResgisterRequst.getUserAccount();
        String userPassword = userResgisterRequst.getUserPassword();
        String checkPassword = userResgisterRequst.getCheckPassword();

        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }

        return userService.userRegister(userAccount, userPassword, checkPassword);

    }

    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequst userLoginRequst, HttpServletRequest request) {

        if (userLoginRequst == null) {
            return null;
        }

        String userAccount = userLoginRequst.getUserAccount();
        String userPassword = userLoginRequst.getUserPassword();


        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return null;
        }

        return userService.userLogin(userAccount, userPassword, request);

    }

    @GetMapping("/search")
    public List<User> searchUser(String username, HttpServletRequest request) {
        if (!isAdmin(request)) {
            return new ArrayList<>();
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isAnyBlank(username)) {
            queryWrapper.like("username", username);
        }

        List<User> userList = userService.list(queryWrapper);
        return userList.stream().map(user -> {
            user.setUserPassword(null);
            return user;
        }).collect(Collectors.toList());
    }

    @PostMapping("/delete")
    public boolean deleteUser(@RequestBody long id, HttpServletRequest request) {

        if (!isAdmin(request)) {
            return false;
        }

        if (id <= 0) {
            return false;
        }
        return userService.removeById(id);
    }

    private boolean isAdmin(HttpServletRequest request) {
        Object userObject = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObject;
        if (user == null || user.getUserRole() != Admin_Role) {
            return false;
        }
        return true;
    }

}
